/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-09-27     huger       the first version
 */
#ifndef APPLICATIONS_APP_MOTOR_MOTOR_H_
#define APPLICATIONS_APP_MOTOR_MOTOR_H_


#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include "bsp/bsp.h"

extern TIM_HandleTypeDef htim3;
extern void MX_TIM3_Init();

#define m1_nsleep_pin   GET_PIN(C,4)     //m1 sleep
#define m1_dir_pin      GET_PIN(B,1)     //m1 dir
#define m1_enable_pin   GET_PIN(C,5)     //m1 enable

#define m2_nsleep_pin   GET_PIN(A,4)     //m2 sleep
#define m2_dir_pin      GET_PIN(A,6)     //m2 dir
#define m2_enable_pin   GET_PIN(A,5)     //m2 enable

#define m3_nsleep_pin   GET_PIN(C,1)     //m3 sleep
#define m3_dir_pin      GET_PIN(C,3)     //m3 dir
#define m3_enable_pin   GET_PIN(C,2)     //m3 enable


#define StepMotor_Dir_Forward(x)    {                                           \
                                        if(x ==0)rt_pin_write(m1_dir_pin,0);   \
                                        else if(x ==1)  rt_pin_write(m2_dir_pin,0);  \
                                        else rt_pin_write(m3_dir_pin,0);   \
                                    }
//
#define StepMotor_Dir_Reversal(x)   {                                        \
                                        if(x ==0)   rt_pin_write(m1_dir_pin,1);  \
                                        else if(x ==1)  rt_pin_write(m2_dir_pin,1); \
                                        else rt_pin_write(m3_dir_pin,1); \
                                    }
//
#define StepMotor_Output_Enable(x)   {                                        \
                                        if(x ==0)   rt_pin_write(m1_enable_pin,0);  \
                                        else if(x ==1)  rt_pin_write(m2_enable_pin,0); \
                                        else rt_pin_write(m3_enable_pin,0); \
                                     }
//
#define StepMotor_Output_Disable(x)  {                                        \
                                        if(x ==0)   rt_pin_write(m1_enable_pin,1);  \
                                        else if(x ==1)  rt_pin_write(m2_enable_pin,1); \
                                        else rt_pin_write(m3_enable_pin,1); \
                                      }


#define FALSE                                 0
#define TRUE                                  1
#define CW                                    0 // 顺时针
#define CCW                                   1 // 逆时针

#define STOP                                  0 // 加减速曲线状态：停止
#define ACCEL                                 1 // 加减速曲线状态：加速阶段
#define DECEL                                 2 // 加减速曲线状态：减速阶段
#define RUN                                   3 // 加减速曲线状态：匀速阶段
#define T1_FREQ                               (168000000/(42)) // 频率ft值
#define FSPR                                  200         //步进电机单圈步数
#define MICRO_STEP                            32          // 步进电机驱动器细分数
#define SPR                                   (FSPR*MICRO_STEP)   // 旋转一圈需要的脉冲数

// 数学常数
#define ALPHA                                 ((float)(2*3.14159/SPR))       // α= 2*pi/spr
#define A_T_x10                               ((float)(10*ALPHA*T1_FREQ))
//#define T1_FREQ_148                           ((float)((T1_FREQ*0.676)/10)) // 0.676为误差修正值
#define T1_FREQ_148                           ((float)((T1_FREQ*0.676)/10)) // 0.676为误差修正值
#define A_SQ                                  ((float)(2*100000*ALPHA))
#define A_x200                                ((float)(200*ALPHA))


#define motor_subdivision             1600 //细分

#define PI                            3.14159265358979f
#define RAD                           180.0/PI

#define rad_per_degree                PI/180.0

#pragma pack(1)
/* 类型定义 ------------------------------------------------------------------*/
typedef struct {
  __IO uint8_t  run_state ;  // 电机旋转状态
  __IO uint8_t  dir ;        // 电机旋转方向
  __IO int32_t  step_delay;  // 下个脉冲周期（时间间隔），启动时为加速度
  __IO uint32_t decel_start; // 启动减速位置
  __IO int32_t  decel_val;   // 减速阶段步数
  __IO int32_t  min_delay;   // 最小脉冲周期(最大速度，即匀速段速度)
  __IO int32_t  accel_count; // 加减速阶段计数值
}MotorRampData;



typedef enum
{
    MOTOR_CW = 0,/*正转*/
    MOTOR_CCW   /*反转*/
}MotorDir;

typedef enum
{
    MOTOR_STOP = 0,
    MOTOR_RUN_START,
    MOTOR_RUN_DOING,
    MOTOR_ABNORMAL
}MotorStatus;

typedef struct
{
    MotorStatus enState;
    MotorDir enDir;

    unsigned char ucRestFlag;       /*复位标识*/
    unsigned char ucStop;           /*停止*/
    unsigned int uiMoveSpeed;       /*移动速度*/
    unsigned int uiAcceleratedSpeed;   /*加速度*/
    unsigned int uiDecelerationSpeed;  /*减速度*/
    unsigned int uiStartVelocity;  /*起始速度*/
    unsigned int uiMaxVelocity;    /*最大速度*/
    unsigned int uiAngle;        /*角度*/
}MotorRun;


extern MotorRun stMotorRun[3];


/* 直线插补参数结构体 */
typedef struct{
     int32_t endpoint_pulse; //到达终点位置需要的脉冲数

     int32_t currentpoint_pluse;//当前位置 脉冲数
     int32_t deviation; //目标位置与当前位置偏差参数

     uint16_t delay;//周期比较值  控制运动速度

     int32_t min_pluse;
     int32_t max_pluse;

     uint8_t self_check_status;

     MotorDir dir;
     int8_t limit_status;

}LinearInterpolation_TypeDef;


#pragma pack()


extern MotorRampData srd[3];
extern LinearInterpolation_TypeDef interpolation_para[3];

extern float_t screw_lead[3];
void motor_init(void);
void StepMotor_AxisMoveRel( int16_t channel,int32_t step, uint32_t accel, uint32_t decel, uint32_t speed);
int8_t InterPolation_Move(int8_t motor_id,int32_t target_pluse,uint32_t time_t);
uint8_t micro_adjust_platform_selfcheck(void);

#ifdef __cplusplus
}
#endif

#endif /* APPLICATIONS_APP_MOTOR_MOTOR_H_ */
